// 代码块 importM
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

// 有状态组件
class HomePage extends StatefulWidget {
  HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePage();
}

class _HomePage extends State<HomePage> {
  int countNum = 0;

  @override
  Widget build(BuildContext context) {
    print(countNum); // setState会重新执行build

    return Scaffold(
        appBar: AppBar(title: const Text("Flutter app")),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                "$countNum",
                style: Theme.of(context).textTheme.displayLarge,
              ),
              const SizedBox(
                height: 100,
              ),
              ElevatedButton(
                  onPressed: () {
                    setState(() {
                      countNum++;
                    });
                  },
                  child: const Text("增加"))
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            setState(() {
              countNum++;
            });
          },
          child: const Icon(Icons.add),
        ));
  }
}
